//
//  a.m
//  ClockIn v2
//
//  Created by Tomy on 12/11/2020.
//

#import "UserViewController.h"
#import "Masonry.h"
#import "Utility.h"

@interface UserViewController() {
    
}

@end



@implementation UserViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // 渐变色
    CAGradientLayer *gradient = [CAGradientLayer layer];
    gradient.type = kCAGradientLayerRadial;
    gradient.frame = self.view.bounds;
    gradient.startPoint = (CGPoint){0.5, 0.5};
    gradient.endPoint = (CGPoint){1.0, 1.0};
    gradient.colors = @[(id)[UIColor orangeColor].CGColor, (id)[UIColor yellowColor].CGColor];
    [self.view.layer insertSublayer:gradient atIndex:0];
    
    // 未登录界面
    [self nonLoginDidLoad];
}

- (void)loginDidLoad {
    // 头像
    UILabel *head = [UILabel new];
    head.text = @"头像";
    CGFloat headWidth = 0.4 * self.view.frame.size.width;
    head.layer.cornerRadius = headWidth * 0.5;
    head.backgroundColor = [UIColor whiteColor];
    head.font = [UIFont systemFontOfSize:40];
    head.clipsToBounds = YES;
    head.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:head];
    [head mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(headWidth);
        make.height.mas_equalTo(headWidth);
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(self.view).offset([Utility getTopBarWidth] + 40);
    }];
    
    
    // 用户信息
    UIView *infoBar = [UIView new];
    [self.view addSubview:infoBar];
    [infoBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(0.5 * self.view.frame.size.width);
        make.height.mas_equalTo(150);
        make.centerX.equalTo(self.view.mas_centerX);
        make.top.equalTo(head.mas_bottom).offset(20);
    }];
    infoBar.layer.borderColor = [UIColor grayColor].CGColor;
    infoBar.layer.borderWidth = 1.0;
    
    NSArray *infoArray = @[@"用户名", @"邮箱", @"电话"];
    UILabel *lastView = nil;
    CGFloat interval = 20;
    for (int i = 0; i < infoArray.count; ++i) {
        UILabel *label = [UILabel new];
        label.text = infoArray[i];
        [infoBar addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i == 0) {
                make.top.equalTo(infoBar).offset(interval);
            }
            else {
                make.top.equalTo(lastView.mas_bottom).offset(interval);
            }
            if (i == infoArray.count - 1) {
                make.bottom.equalTo(infoBar).offset(-interval);
            }
            make.left.equalTo(infoBar).offset(10);
        }];
        
        lastView = label;
    }
    
    // 关于标签
    UILabel *aboutLabel = [UILabel new];
    aboutLabel.text = @"关于";
    [aboutLabel sizeToFit];
    [self.view addSubview:aboutLabel];
    [aboutLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(infoBar.mas_bottom).offset(20);
        make.left.equalTo(self.view).offset(30);
    }];
    
    
    // 关于详情
    UIView *aboutBar = [UIView new];
    [self.view addSubview:aboutBar];
    [aboutBar mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(250);
        make.centerX.equalTo(self.view.mas_centerX);
        make.left.equalTo(aboutLabel);
        make.top.equalTo(aboutLabel.mas_bottom).offset(5);
    }];
    aboutBar.layer.borderColor = [UIColor grayColor].CGColor;
    aboutBar.layer.borderWidth = 1.0;
    
    NSArray *aboutArray = @[@"版本", @"隐私和cookie", @"清除缓存", @"同步"];
    interval = 30;
    lastView = nil;
    for (int i = 0; i < aboutArray.count; ++i) {
        UILabel *label = [UILabel new];
        label.text = aboutArray[i];
        [aboutBar addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            if (i == 0) {
                make.top.equalTo(aboutBar).offset(interval);
            }
            else {
                make.top.equalTo(lastView.mas_bottom).offset(interval);
            }
            if (i == aboutArray.count - 1) {
                make.bottom.equalTo(aboutBar).offset(-interval);
            }
            make.left.equalTo(aboutBar).offset(10);
        }];
        
        lastView = label;
    }
}

- (void)nonLoginDidLoad {
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeSystem];
    [btn setTitle:@"登录" forState:UIControlStateNormal];
    // 触发登录事件
    [btn addTarget:self action:@selector(loginTrigger) forControlEvents:UIControlEventTouchUpInside];
    btn.backgroundColor = [UIColor whiteColor];
    CGFloat width = self.view.frame.size.width * 0.6;
    btn.layer.cornerRadius = width * 0.5;
    btn.titleLabel.font = [UIFont systemFontOfSize:60];
    [self.view addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.width.mas_equalTo(width);
        make.height.mas_equalTo(width);
        make.centerX.equalTo(self.view.mas_centerX);
        make.centerY.equalTo(self.view.mas_centerY);
    }];
}

- (void)loginTrigger {
    for (UIView *subView in self.view.subviews) {
        [subView removeFromSuperview];
    }
    [self loginDidLoad];
}

@end
